package com.wyl.server.firstpage


import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableInt
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.tabs.TabLayout
import com.wuhenzhizao.titlebar.widget.CommonTitleBar.ACTION_RIGHT_BUTTON
import com.wyl.base.PublishTypePage
import com.wyl.libbase.adapter.SimpleTabSelectedListener
import com.wyl.libbase.base.PageFragment
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.extend.onclick
import com.wyl.libbase.extend.openActivity
import com.wyl.libbase.extend.toast
import com.wyl.libbase.utils.CountDownUtils
import com.wyl.server.R
import com.wyl.server.databinding.ServiceFragmentBinding
import com.wyl.server.detail.DetailServiceActivity
import com.wyl.server.search.SearchListActivity
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.MultiTypeAdapter
import io.ditclear.bindingadapter.SingleTypeAdapter
import kotlinx.android.synthetic.main.service_fragment.*


class ServiceFragment : PageFragment<ServiceFragmentBinding, Any>() {

    val types = ObservableArrayList<String>()

    override fun getLayoutId(): Int = R.layout.service_fragment

    override fun initView(view: View) {
        binding.vm = this

        titleBar.setListener { _, action, _ ->
            when (action) {
                ACTION_RIGHT_BUTTON -> {
                    openActivity(SearchListActivity::class.java)
                }
                else -> {
                }
            }
        }

        val list = mutableListOf<String>()
        list.add("学好Java、Android、C#、C、ios、html+css+js")
        list.add("走遍天下都不怕！！！！！")
        list.add("不是我吹，就怕你做不到，哈哈")
        list.add("superluo")
        list.add("你是最棒的，奔跑吧孩子！")
        tv_banner.setDatas(list)
        tv_banner.setItemOnClickListener { data, _ ->
            context?.toast(data)
        }

        rvType.apply {
            layoutManager = GridLayoutManager(context, 5)
            adapter = SingleTypeAdapter(context, R.layout.service_item1, types).apply {
                itemPresenter = object : ItemClickPresenter<String> {
                    override fun onItemClick(v: View, item: String) {

                    }
                }
            }
        }

        tab.addOnTabSelectedListener(object : SimpleTabSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab) {
                type2 = tab.text.toString()
                refreshLayout.startRefresh()
            }
        })

        rvInfo.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
            addItemDecoration(RecyclerViewDivider(context, RecyclerViewDivider.VERTICAL))
        }

        add.setOnClickListener { openActivity(PublishTypePage) }

    }

    private val mAdapter by lazy {
        MultiTypeAdapter(activity!!, dataSource, object : MultiTypeAdapter.MultiViewTyper {
            override fun getViewType(item: Any): Int =
                when (type2) {
                    "服务" -> 1
                    "互动" -> 3
                    else -> 1
                }
        }).apply {
            addViewTypeToLayoutMap(1, R.layout.service_item2)
            addViewTypeToLayoutMap(2, R.layout.information_item)
            itemPresenter = object : ItemClickPresenter<Any> {
                override fun onItemClick(v: View, item: Any) {
                    openActivity(DetailServiceActivity::class.java)
                }
            }
        }
    }

    var type2 = "服务"


    var topIndex = ObservableInt(0)

    fun checkTopIndex(i: Int) {
        topIndex.set(i)
    }

    override fun loadData() {
        refresh()
    }

    override fun refresh() {
        CountDownUtils.delay(1) {
            dataSource.clear()
            refresh.set(false)
            repeat(6) {
                dataSource.add(type2)
            }
        }

    }

    override fun loadMore() {
        CountDownUtils.delay(1) {
            loadMore.set(false)
            repeat(4) { dataSource.add("") }
        }
    }

}
